import axios, { AxiosInstance, AxiosRequestConfig, AxiosStatic } from 'axios';

class AxiosUtils{

    private request: AxiosInstance;

    constructor(axios: AxiosStatic){
        this.request = axios.create({
            timeout: 5000,
            // baseURL: "https://music-zlq.vercel.app"
            baseURL: "http://localhost:3000"
        })
    }

    requestInterceptors(request: AxiosInstance): any{
        request.interceptors.request.use(
            (confirm: AxiosRequestConfig<any>) => {
                console.log('我是请求拦截器, 发送请求成功');
                return confirm
            },
            (error:string) => {
                console.log('我是请求拦截器, 发送请求失败');
                return Promise.reject(error)
            }
         )
    }

    responseInterceptors(request: AxiosInstance): any{
        request.interceptors.response.use(
            (response: any) => {
                console.log('我是响应拦截器， 响应数据返回成功');
                return response
            },
            (error: string) =>{
                console.log('我是响应拦截器，响应数据返回失败');
                return Promise.reject(error)
            }
        )
    }

    getRequest(){
        this.requestInterceptors(this.request);
        this.responseInterceptors(this.request)
        return this.request
    }
}

export default new AxiosUtils(axios).getRequest();